package com.lagou.edu.service.impl;

import com.lagou.edu.api.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.apache.dubbo.config.annotation.Service;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${email.from}")
    private String from;

    @Override
    public Boolean email(String email, String code) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(email);
            message.setSubject("验证码");
            message.setText("您的验证码是：" + code);
            new Thread(()->{
                mailSender.send(message);
            }).start();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
